# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
import pymysql


class DangdangPipeline(object):
    def process_item(self, item, spider):
        conn = pymysql.connect(host = 'localhost', user = 'root', passwd = 'dianjoy', db = 'test1', port = 3306, charset = 'utf8')
        for i in range(0, len(item['title'])):
            title = item['title'][i]
            link = item['link'][i]
            comment1 = item['comment'][i]
            comment = int(comment1.replace('条评论', ''))
            press = item['press'][i]
            sql = '''insert into dangdang(title, link, comment, press) values ('%s', '%s', %d, '%s');''' % (title, link, comment, press)
            try:
                conn.query(sql)
            except pymysql.Error as e:
                print('Error:%s ' % str(e))
        conn.commit()
        conn.close()
        return item
